home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / grafik / cgazv5n3 / topdown2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-27  |  4.9 KB  |  170 lines

  1. /* Listing 5  TOPDOWN2.C -- Cleaned-up Parser */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include "lex.h"
  7.  
  8. void statement                  ( void );
  9. void expression_and_predicate   ( void );
  10. void mult_expr_and_predicate    ( void );
  11. void factor                     ( void );
  12. void error                      (char *fmt,...);
  13.  
  14. #ifdef PTRACE
  15.      static int Rdepth = 0;
  16.      int        rdepth(void)    {return( Rdepth * 8 );}
  17. #    define     trace(name)     printf("%*s%s\n", Rdepth++ * 8, "",  name)
  18. #    define     untrace(name)   (--Rdepth)
  19. #else
  20. #    define trace(name)         /* empty */
  21. #    define untrace(name)       /* empty */
  22. #endif
  23. /*----------------------------------------------------------------------*/
  24. void statement( void )
  25. {
  26.      /* statement->WHILE LP expression RP statement     WHILE
  27.       * statement->expression SEMI                      LP SEMI ID NUMBER
  28.       *
  29.       * Modified to eliminate tail recursion.
  30.       */
  31.  
  32.     trace("statement");
  33.     if( match(LP) || match(SEMI) || match(ID) || match(NUMBER) )
  34.     {
  35.         expression_and_predicate();
  36.  
  37.         if( match(SEMI) ) advance();
  38.         else              error("Inserting missing semicolon.");
  39.     }
  40.     else if( !match(WHILE) )
  41.         error( "while loop or expression expected\n" );
  42.  
  43.     else while( match(WHILE) )
  44.     {
  45.         advance();
  46.  
  47.         if( match(LP) ) advance();
  48.         else            error("Inserting missing left parenthesis.");
  49.  
  50.         expression_and_predicate();
  51.  
  52.         if( match(RP) ) advance();
  53.         else            error("Inserting missing right parenthesis.");
  54.     }
  55.  
  56.     untrace("statement");
  57. }
  58. /*----------------------------------------------------------------------*/
  59. void expression_and_predicate( void )
  60. {
  61.      /* expression->mult_expr predicate            LP ID NUMBER
  62.       * expression->(epsilon)                      RP SEMI
  63.       *
  64.       * Modified to incorporate the predicate productions and also
  65.       * to eliminate the tail recursion in the original predicate();
  66.       */
  67.  
  68.     trace("expression_and_predicate");
  69.  
  70.     if( match(RP) || match(SEMI) )
  71.         ;                                       /* epsilon */
  72.  
  73.     else if( !(match(LP) || match(ID) || match(NUMBER)) )
  74.         error( "expression expected\n" );
  75.     else
  76.     {
  77.         mult_expr_and_predicate();
  78.  
  79.          /* predicate->PLUS mult_expr predicate    PLUS
  80.           * predicate->MINUS mult_expr predicate   MINUS
  81.           * predicate->(epsilon)                   RP SEMI
  82.           */
  83.  
  84.         if( match(RP) || match(SEMI) )
  85.             ;                                   /* epsilon */
  86.  
  87.         else if( !(match(PLUS) || match(MINUS)) )
  88.             error("operator or statement-terminator expected\n");
  89.  
  90.         else while( match(PLUS) || match(MINUS) )
  91.         {
  92.             advance();
  93.             mult_expr_and_predicate();
  94.         }
  95.     }
  96.     untrace("expression_and_predicate");
  97. }
  98. /*----------------------------------------------------------------------*/
  99. void mult_expr_and_predicate( void )
  100. {
  101.      /* mult_expr->factor mult_predicate        LP ID NUMBER
  102.       *
  103.       * Modified to eliminate chain to mult_predicate() and also
  104.       * eliminate tail recursion in mult_predicate();
  105.       */
  106.  
  107.     trace("mult_expr_and_predicate");
  108.  
  109.     if( !(match(LP) || match(ID) || match(NUMBER)) )
  110.         error( "expected number identifier or open parenthesis\n" );
  111.     else
  112.     {
  113.         factor();
  114.  
  115.          /* mult_predicate->STAR factor mult_predicate  STAR
  116.           * mult_predicate->SLASH factor mult_predicate SLASH
  117.           * mult_predicate->(epsilon)     RP PLUS MINUS SEMI
  118.           */
  119.  
  120.         if( match(RP) || match(PLUS) || match(MINUS) || match(SEMI) )
  121.             ;           /* epsilon */
  122.         else if( !(match(STAR) || match(SLASH)) )
  123.             error("operator expected\n");
  124.  
  125.         else while( match(STAR) || match(SLASH) )
  126.         {
  127.             advance();
  128.             factor();
  129.         }
  130.     }
  131.  
  132.     untrace("mult_expr_and_predicate");
  133. }
  134. /*----------------------------------------------------------------------*/
  135. void factor( void )
  136. {
  137.     /* factor->NUMBER                           NUMBER
  138.      * factor->ID                               ID
  139.      * factor->LP expression RP                 LP
  140.      */
  141.  
  142.     trace( "factor" );
  143.  
  144.     if( match(NUMBER) || match(ID) )
  145.         advance();
  146.     else if( match(LP) )
  147.     {
  148.         advance();
  149.         expression_and_predicate();
  150.  
  151.         if( match(RP) ) advance();
  152.         else            error("Inserting missing right parenthesis.");
  153.     }
  154.  
  155.     untrace( "factor" );
  156. }
  157. /*----------------------------------------------------------------------*/
  158. void error( char *fmt, ... )
  159. {
  160.     va_list     args;
  161.     va_start( args, fmt );
  162.     vfprintf( stderr, fmt, args );
  163.     va_end( args );
  164. }
  165. /*----------------------------------------------------------------------*/
  166. int main()
  167. {
  168.     statement();
  169.     return 0;
  170. }